package top.carrotguo.weibo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Process;
import android.util.DisplayMetrics;
import android.view.View;

import top.carrotguo.weibo.MyApplication;

/**
 * Created by carrot on 2018-07-03.
 */

public class UIUtils {

    /**
     * 获取Application的Context
     * @return
     */
    public static Context getContext(){
        return MyApplication.getContext();
    }

    public static Handler getHandler(){
        return MyApplication.getHandler();
    }

    /**
     * 返回主线程id
     * @return
     */
    public static int getMainThreadId(){
        return MyApplication.getMainThreadID();
    }

    /////////////////////////获取资源文件/////////////////////////////

    /**
     * 返回String
     * @param id
     * @return
     */
    public static String getString(int id){
        return getContext().getResources().getString(id);
    }

    /**
     * 返回字符串数组
     * @param id
     * @return
     */
    public static String[] getStringArray(int id){
        return getContext().getResources().getStringArray(id);
    }

    /**
     * 返回颜色
     * @param id
     * @return
     */
    public static int getColor(int id){
        return getContext().getResources().getColor(id);
    }

    public static ColorStateList getColorStateList(int id){
        return getContext().getResources().getColorStateList(id);
    }

    /**
     * 返回Drawable对象
     * @param id
     * @return
     */
    public static Drawable getDrawable(int id){
        return getContext().getResources().getDrawable(id);
    }

    /**
     * 返回像素值
     * @param id
     * @return
     */
    public static int getDimen(int id){
        return getContext().getResources().getDimensionPixelSize(id);
    }

    /**
     * dip转px
     * @param dip
     * @return
     */
    public static int dip2px(float dip){
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int)(density*dip+0.5f);         //四舍五入
    }

    /**
     *px转dip
     * @param px
     * @return
     */
    public static int px2dip(int px){
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int)(px/density);
    }

    public static int getScreenWidth(){
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        return dm.widthPixels;
    }

    /////////////////////////加载布局文件/////////////////////////////
    public static View inflate(int id){
        return View.inflate(getContext(),id,null);
    }

    /////////////////////判断程序是否运行在主线程//////////////////////
    public static boolean isRunOnMainThread(){
        int threadID = Process.myTid();
        if (threadID == getMainThreadId()) {
            return true;
        }
        return false;
    }

    /////////////////////运行到ui线程//////////////////////
    public static void runOnUIThread(Runnable runnable){
        if (isRunOnMainThread()) {
            runnable.run();
        } else {
            getHandler().post(runnable);        //发送到消息队列  让looper去执行
        }
    }

}
